<template>
  <view class="home">
    <view class="title">
      <view class="title-quest">
        <text
          @click="goOutApply"
          class="iconfont icon-shenqing1 title-icon"
        ></text>
        <view @click="goOutApply" class="title-quest-text">出入申请</view>
      </view>
      <view class="title-scan">
        <text
          @click="scanCode"
          class="iconfont icon-saoyisao title-icon"
        ></text>
        <view @click="scanCode" class="title-scan-text">扫一扫</view>
      </view>
    </view>
    <serviceHome></serviceHome>
    <NoticeHome :noticeList="noticeList"></NoticeHome>
  </view>
</template>

<script>
import serviceHome from "../../components/serviceHome/serviceHome.vue";
import NoticeHome from "../../components/noticeHome/noticeHome.vue";
export default {
  data() {
    return {
      noticeList: [],
    };
  },
  components: {
    serviceHome,
    NoticeHome,
  },
  onShow() {
    uni.getStorage({
      key: "openid",
      success: function (res) {
        console.log("已登录");
      },
      fail: function (rej) {
        uni.showModal({
          title: "请进行登录使用",
          content: "",
          showCancel: true,
          success: ({ confirm, cancel }) => {
            if (confirm) {
              // console.log("dengl");
              uni.switchTab({
                url: "../my/my",
                fail: (err) => {
                  console.log(err);
                },
              });
              // uni.navigateTo({ url: "pages/my/my" });
            }
            if (cancel) {
              console.log("dengl");
              uni.switchTab({
                url: "../my/my",
                fail: (err) => {
                  console.log(err);
                },
              });
            }
          },
        });
      },
    });
    this.requestData();
  },
  methods: {
    requestData() {
      // console.log(this.$route.path);
      let pages = getCurrentPages();
      if (pages[0].route == "pages/home/homeIndex") {
        this.$MyRequest({
          url: "/notice-list6",
        })
          .then((res) => {
            // console.log(res);
            this.noticeList = res.data.data;
            // console.log(this.noticeList);
            this.noticeList = this.noticeList.sort((a, b) => {
              return a.id < b.id ? 1 : -1;
            });
            // console.log(that.tableList);
          })
          .catch((rjt) => {
            console.log(rjt);
          });
      }
    },
    scanCode() {
      const that = this;
      uni.scanCode({
        success: function (res) {
          console.log(res.result);
          let url = res.result.split("/")[3];
          console.log(url);
          that
            .$MyRequest({
              url: "/" + url,
            })
            .then((resolve) => {
              console.log(resolve);
              if (resolve.data.mesg == "扫码成功") {
                console.log("chengggg");
                uni.navigateTo({ url: "/pages/home/SandCode/SandCodeHome" });
              } else {
                uni.showToast({
                  title: "扫码失败",
                  icon: "none",
                  duration: 2000,
                });
              }
            })
            .catch((error) => {
              uni.showToast({
                title: "扫码失败",
                icon: "none",
                duration: 2000,
              });
            });
          // uni.navigateTo({
          //   url: "https://www.baidu.com/",
          // });
          // // location.href="res.result"
          // console.log(res.result)
          // location.href = `${res.result}`;
          // console.log(JSON.stringify(res));
        },
        fail: function (rej) {
          uni.showToast({
            title: "扫码失败",
            icon: "none",
            duration: 2000,
          });
        },
      });
    },
    goOutApply() {
      uni.navigateTo({
        url: "/pages/home/service/GoOutApply/GoOutApply",
      });
    },
  },
};
</script>

<style lang="scss">
@import "@/static/styles/mixin.scss";

page {
  background-color: $uni-color-indexBg;
}
.home {
  width: 100%;
  // height: 100%;
  background-color: $uni-color-indexBg;
  background: $uni-color-indexBg;
  .title {
    height: 200rpx;
    background-color: $uni-bg-color;
    display: flex;
    justify-content: space-around;
    align-items: center;
    color: #ffffff;
    .title-icon {
      font-size: 70rpx;
    }
    .title-quest {
      text {
        @include center;
      }
      .title-quest-text {
      }
    }
    .title-scan {
      text {
        @include center;
      }
    }
  }
}
</style>
